#include "smaterhome.h"
#include "ui_smaterhome.h"

SmaterHome::SmaterHome(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SmaterHome),
      m_serialPort(new QSerialPort(this)),
      button1State(false),  // 初始化为关闭状态
      button2State(false),
      button3State(false),
      button4State(false)
{
    ui->setupUi(this);


     slotOpenSerialPort();


     setButtonIcon(ui->pushButton, ":/img/吊灯-关.png");
     setButtonIcon(ui->pushButton_2,":/img/百叶窗-叶子-关.png");
     setButtonIcon(ui->pushButton_3,":/img/风扇-关.png");
     setButtonIcon(ui->pushButton_4,":/img/空调-关.png");


     // 连接按钮点击信号到各自的槽函数
     connect(ui->pushButton, &QPushButton::clicked, this, &SmaterHome::slotToggleButton1);
     connect(ui->pushButton_2, &QPushButton::clicked, this, &SmaterHome::slotToggleButton2);
     connect(ui->pushButton_3, &QPushButton::clicked, this, &SmaterHome::slotToggleButton3);
     connect(ui->pushButton_4, &QPushButton::clicked, this, &SmaterHome::slotToggleButton4);

 }

void SmaterHome::setButtonIcon(QPushButton *button, const QString &imagePath) {
    QPixmap pixmap(imagePath);
    if (pixmap.isNull()) {
        qDebug() << "图片加载失败:" << imagePath;
        return;
    }
    button->setIcon(QIcon(pixmap));
    button->setIconSize(pixmap.size());
}

 void SmaterHome::slotLoadSerialPort()
 {
     QStringList m_serialPortName;
     foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) //获取有效的串口列表
     {
         m_serialPortName << info.portName();
         qWarning()<<"serialPortName:"<<info.portName();
     }
 }

 // 切换按钮1状态
 void SmaterHome::slotToggleButton1()
 {
     if (m_serialPort->isOpen()) {
         if (button1State) {
             QString info = "关闭吊灯";
             m_serialPort->write(info.toLocal8Bit());
             setButtonIcon(ui->pushButton, ":/img/吊灯-关.png");
         } else {
             QString info = "打开吊灯";
             m_serialPort->write(info.toLocal8Bit());
             setButtonIcon(ui->pushButton, ":/img/吊灯-开.png");
         }
         button1State = !button1State;  // 切换状态
     }
 }
 // 切换按钮2状态
 void SmaterHome::slotToggleButton2()
 {
     if (m_serialPort->isOpen()) {
         if (button2State) {
             QString info = "关闭百叶窗";
             m_serialPort->write(info.toLocal8Bit());
             setButtonIcon(ui->pushButton_2, ":/img/百叶窗-叶子-关.png");
         } else {
             QString info = "打开百叶窗";
             m_serialPort->write(info.toLocal8Bit());
             setButtonIcon(ui->pushButton_2, ":/img/百叶窗-叶子-开.png");
         }
         button2State = !button2State;  // 切换状态
     }
 }
 // 切换按钮3状态
 void SmaterHome::slotToggleButton3()
 {
     if (m_serialPort->isOpen()) {
         if (button3State) {
             QString info = "关闭风扇";
             m_serialPort->write(info.toLocal8Bit());
             setButtonIcon(ui->pushButton_3, ":/img/风扇-关.png");
         } else {
             QString info = "打开风扇";
             m_serialPort->write(info.toLocal8Bit());
             setButtonIcon(ui->pushButton_3, ":/img/风扇-开.png");
         }
         button3State = !button3State;  // 切换状态
     }
 }
 // 切换按钮4状态
 void SmaterHome::slotToggleButton4()
 {
     if (m_serialPort->isOpen()) {
         if (button4State) {
             QString info = "关闭空调";
             m_serialPort->write(info.toLocal8Bit());
             setButtonIcon(ui->pushButton_4, ":/img/空调-关.png");
         } else {
             QString info = "打开空调";
             m_serialPort->write(info.toLocal8Bit());
             setButtonIcon(ui->pushButton_4, ":/img/空调-开.png");
         }
         button4State = !button4State;  // 切换状态
     }
 }


 void SmaterHome::slotCloseSerialPort()
 {
     if (m_serialPort)
         m_serialPort->close();
 }

 void SmaterHome::slotOpenSerialPort()
 {
     m_serialPort = new QSerialPort(this);//新建一个窗口对象

     if(m_serialPort->isOpen())//判断对象是否已经打开，如果是就关掉
     {
         m_serialPort->clear();
         m_serialPort->close();
     }
     //设置串口名字
     m_serialPort->setPortName("COM2");
     //打开串口
     if (!m_serialPort->open(QIODevice::ReadWrite))//打开COM2串口，并且设置为读写状态
     {
         return;
     }

     m_serialPort->setBaudRate(9600);//波特率
     m_serialPort->setDataBits(QSerialPort::Data8);//数据位
     m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//数据流控制（暂时不管）

     m_serialPort->setParity(QSerialPort::NoParity);//奇偶校验位
     m_serialPort->setStopBits(QSerialPort::OneStop);//停止位


     //把串口的可读信号和槽函数连接
     connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(slotReadData()));

     connect(ui->pushButton, &QPushButton::clicked, this, &SmaterHome::slotSend);

 }

 void SmaterHome::slotReadData()
 {
     QByteArray data = m_serialPort->readAll();  // 读取串口数据
        QString decodedData = QString::fromLocal8Bit(data);   // 使用UTF-8解码
        qWarning() << "接收数据：" << decodedData;  // 输出解码后的字符串

        if (decodedData.contains("打开吊灯"))
        {
            QPixmap openPixmap(":/img/吊灯-开.png"); // 开启状态的图片
            ui->pushButton->setIcon(QIcon(openPixmap));
        }
        else if (decodedData.contains("关闭吊灯"))
        {
            QPixmap closePixmap(":/img/吊灯-关.png"); // 关闭状态的图片
            ui->pushButton->setIcon(QIcon(closePixmap));
        }
        else if (decodedData.contains("打开风扇"))
        {
            QPixmap openPixmap_3(":/img/风扇-开.png"); // 关闭状态的图片
            ui->pushButton_3->setIcon(QIcon(openPixmap_3));
        }
        else if (decodedData.contains("关闭风扇"))
        {
            QPixmap closePixmap_3(":/img/风扇-关.png"); // 关闭状态的图片
            ui->pushButton_3->setIcon(QIcon(closePixmap_3));
        }
        else if (decodedData.contains("打开百叶窗"))
        {
            QPixmap openPixmap_2(":/img/百叶窗-叶子-开.png"); // 关闭状态的图片
            ui->pushButton_2->setIcon(QIcon(openPixmap_2));
        }
        else if (decodedData.contains("关闭百叶窗"))
        {
            QPixmap closePixmap_2(":/img/百叶窗-叶子-关.png"); // 关闭状态的图片
            ui->pushButton_2->setIcon(QIcon(closePixmap_2));
        }
        else if (decodedData.contains("打开空调"))
        {
            QPixmap openPixmap_4(":/img/空调-开.png"); // 关闭状态的图片
            ui->pushButton_4->setIcon(QIcon(openPixmap_4));
        }
        else if (decodedData.contains("关闭空调"))
        {
            QPixmap closePixmap_4(":/img/空调-关.png"); // 关闭状态的图片
            ui->pushButton_4->setIcon(QIcon(closePixmap_4));
        }
 }
 void SmaterHome::slotSend()
 {
     if (m_serialPort)
     {
         if (!m_serialPort->isOpen()) {
             qWarning() << "请打开一个串口";
             return;
         }

         QString info = "hello";
         QByteArray sendBuf;
         //把字符串转为字节
         sendBuf.append(info);// = info.toLocal8Bit();
         m_serialPort->write(sendBuf);
     }
 }
SmaterHome::~SmaterHome()
{
    delete ui;
}

void SmaterHome::on_pushButton_clicked()
{
    slotSend();
}
